﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using FuncWorks.XNA.XTiled;
using fess.Graphics;
using Microsoft.Xna.Framework.Graphics;
using fess.RPG;
using fess.Screens;
using Microsoft.Xna.Framework.Input;

namespace fess.Maps
{
  class Home : GenericMap
  {
    Vector2 bed;
    bool firstTime = true;

    public Home(FessGame game)
      : base(game, game.Content.Load<Map>("home"))
    {
      drawObjects = true;
      AddCharacter(game.daenur, new Vector2(5 * 32, 5 * 32), false);
      game.daenur.AddAI(new GotoAI(game.daenur, game.daenur.Character.Position, new Vector2(7, 7) * 32));
      bed = map.ObjectLayers[0].MapObjects.FirstOrDefault(mo => mo.Name == "bed").Bounds.Center.ToVector2();
    }

    public override void Activate()
    {
      game.fess.Character.Position = new Vector2(16, 17) * 32;
      base.Activate();

      if (firstTime)
      {
        firstTime = false;
        game.fess.Say("Настало время для моей летней практики.\nНадо поговорить с Даэнуром, он выдаст мне\nмоё задание.", 2);
      }
    }

    public override bool Update(GameTime gameTime)
    {
      if(!base.Update(gameTime))
        return false;

      if (interaction == null && (game.fess.Character.Position - bed).Length() < 50 && game.fess.Health < game.fess.MaxHealth)
      {
        interaction = "Отдохнуть";

        if (Misc.IsKeyJustPressed(Keys.E))
        {
          game.fess.Say("Посплю немного.");
          Fade(() => game.fess.Health = game.fess.MaxHealth);
        }
      }

      return true;
    }

    protected override bool ExitEngaged(MapObject exit)
    {
      List<Answer> destinations = new List<Answer>();
      destinations.Add(new Answer("Таверна", () => game.tavern.Activate()));
      if(game.CemeteryQuest != FessGame.CemeteryQuestStatus.Unknown)
        destinations.Add(new Answer("Кладбище", () => game.cemetery.Activate()));
      if (game.CaveQuest != FessGame.CaveQuestStatus.Unknown)
        destinations.Add(new Answer("Пещера", () => game.caveOut.Activate()));

      game.AddActivity(new Dialog(game.fess,
          () => { },
          () => { },
        new Dialog.Line(game.fess, "Пора в путь.", destinations.ToArray())));

      return true;
    }
  }
}
